package tuplespace;

/**
 * <p>Title: TupleSpace</p>
 * <p>Description: TupleSpace</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: DII - DISMI</p>
 * @author Marco Mamei
 * @version 1.0
 */

import java.io.*;
import emulator.*;

public class Start
{
 private static final int EMULATOR = 0;
 private static final int REAL = 1;

 private static int mode = EMULATOR;

 public static void main(String[] args)
 {
  switch(mode)
  {
   case EMULATOR  : startEmulator("../ConfigFile/ts.xml");
                    break;

   case REAL      : startReal();
                    break;
  }
 }


 /*****************************************************************************/
 /*                         START EMULATOR                                    */
 /*****************************************************************************/

 private static void startEmulator(String configFile)
 {
  int NUM_PARAM = 22;
  String[] args = new String[NUM_PARAM];
  try
  {
   BufferedReader in = new BufferedReader(new FileReader(configFile));
   int index = 0;
   String line;
   while( (line = in.readLine()) != null)
   {
    if (!line.trim().startsWith("<")) // skip xml tags
    {
     args[index] = line.trim();
     index++;
    }
   }
   in.close();
  }
  catch(Exception e){e.printStackTrace();}

  Emulator.startFromCommandLine(args);
 }

 /*****************************************************************************/
 /*                               START REAL                                  */
 /*****************************************************************************/

 private static void startReal()
 {
  real802_11.Peer peer = new real802_11.Peer();
  System.out.println("il peer creato ha valore: "+peer);
  /* install agent on the peer */
  tuplespace.agent.AgentTS ag = new tuplespace.agent.AgentTS(peer);
  /* start executing the agent */
  ag.showAgentFrame();
  int time = 0;
  while(true)
  {
   ag.step(time);
   try{Thread.currentThread().sleep(100);}catch(Exception e){e.printStackTrace();}
   time++;
  }
 }
}